package com.lottery.api.controller.activity;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lottery.api.annotation.CheckLoginAop;
import com.lottery.api.annotation.CoerceLoginAop;
import com.lottery.api.service.MemberInfoService;
import com.lottery.common.service.*;
import com.lottery.api.service.EventsInfoService;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.entity.*;
import com.lottery.common.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.lottery.common.contants.Constants.*;
import static com.lottery.common.contants.ReQueryResult.TYPE_ONE_HUNDRED_FIVE_STR;

/**
 * @author o551wwl
 */
@RestController
@RequestMapping("/recommend")
@Slf4j
//@Api(value = "/recommend",tags="获取推荐")
public class RecommendController {
    private ReQueryResult result;

    @Resource
    private OrderInfoService orderInfoService;
    @Resource
    private CustomInfoService customInfoService;
    @Autowired
    private MemberInfoService mis;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MemberInfoSubService miss;
    @Autowired
    private EventsInfoService eis;
    @Autowired
    private CompetitionRecordService competitionRecordService;
    @Autowired
    private ShortReportInfoService shortReportInfoService;
    @Autowired
    private ForecastInfoService forecastInfoService;
    @Autowired
    private FocusMemberRecordService focusMemberRecordService;


//
//    @ApiOperation(value = "今日热门推荐")
//    @ApiImplicitParam(name = "limit",value = "长度",dataType="Integer")
//    @RequestMapping(value = "/hot/{limit}", method = RequestMethod.GET)
//    public ReQueryResult<List<EventsInfo>> hot(@PathVariable Integer limit){
//        result = new ReQueryResult();
//        if (limit==null){
//            limit=4;
//        }
//        QueryWrapper<EventsInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("is_hot",TYPE_ZERO);
//        queryWrapper.eq("type",TYPE_ONE);
//        queryWrapper.eq("is_del",TYPE_ONE);
//
//        queryWrapper.apply(" date_format(curdate(),'%Y-%m-%d')=date_format(events_time,'%Y-%m-%d') ");
//        queryWrapper.apply(" 1=1  order by id desc LIMIT  "+limit);
//        List<EventsInfo> list=eis.list(queryWrapper);
//        result.setData(list);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//    @ApiOperation(value = "热门推荐列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "eventsType",value = "1足球 2 篮球 ",dataType="Integer"),
//            @ApiImplicitParam(name = "index",value = "页码 ",dataType="Integer"),
//            @ApiImplicitParam(name = "length",value = "长度",dataType="Integer")
//    })
//    @RequestMapping(value = "/hotList", method = RequestMethod.GET)
//    public ReQueryResult<IPage<EventsInfo>> hotList(Integer index,Integer length,Integer eventsType){
//        result = new ReQueryResult();
//        QueryWrapper<EventsInfo> queryWrapper = new QueryWrapper<>();
//        if (eventsType!=null) {
//            queryWrapper.eq("events_type",eventsType);
//        }
//        queryWrapper.eq("is_hot",TYPE_ZERO).eq("type",TYPE_ONE)
//        .eq("is_del",TYPE_ONE).orderByDesc("id");
//
//
//        Page<EventsInfo> page = new Page<>(index,length);
//        IPage<EventsInfo> list=eis.page(page,queryWrapper);
//        result.setData(list);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//    @ApiOperation(value = "推荐列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "eventsTime",value = "日期 ",dataType="Date"),
//            @ApiImplicitParam(name = "eventsType",value = "1足球 2 篮球 ",dataType="Integer"),
//            @ApiImplicitParam(name = "type",value = "1今日推荐 2 会员 ",dataType="Integer")
//    })
//    @RequestMapping(value = "/today", method = RequestMethod.GET)
//    @CheckLoginAop
//    public ReQueryResult<List<EventsInfo>> today(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
//                                                 Integer eventsType,Integer type,Date eventsTime){
//        result = new ReQueryResult();
//        log.info("eventsType is {} type is {} eventsType is {} ",eventsType,type,eventsTime);
//        QueryWrapper<EventsInfo> queryWrapper = new QueryWrapper<>();
//        NutMap nutMap=new NutMap();
//        nutMap.setv("eventsType",eventsType).setv("type",type).setv("eventsTime",eventsTime);
//        nutMap.setv("memberId",memberInfo.getId());
//        List<EventsInfo> list=eis.selectEventsList(nutMap);
//
//        //建议前端处理
//        if (memberInfo.getId()!=null){
//            Date date=new Date();
//            if (memberInfo.getExpiredTime()!=null&&memberInfo.getExpiredTime().after(date)){
//                result.setIsExpired(TYPE_ZERO);
//            }
//        }
//
//        result.setData(list);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//
//    @ApiOperation(value = "推荐详情")
//    @ApiImplicitParam(name = "id",value = "推荐id ",dataType="Integer")
//    @RequestMapping(value = "/today/todayDetail", method = RequestMethod.GET)
//    @CheckLoginAop
//    public ReQueryResult<EventsInfo> todayDetail(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo, Integer id){
//        result = new ReQueryResult();
//        QueryWrapper<EventsInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("is_del",TYPE_ONE).eq("id",id);
//        NutMap nutMap=new NutMap();
//        nutMap.setv("memberId",memberInfo.getId());
//        nutMap.setv("id",id);
//        log.info("nutmap is {}", JSON.toJSONString(nutMap));
//        MemberInfo info=mis.getById(memberInfo.getId());
//        EventsInfo eventsInfo=eis.selectEventsById(nutMap);
//        Date date=new Date();
//        if (eventsInfo!=null&&eventsInfo.getType()==TYPE_TWO&&eventsInfo.getWhether()!=1&&
//                (info.getExpiredTime()==null ||info.getExpiredTime().before(date))){
//            result.setResultCode(TYPE_ONE_HUNDRED_FIVE_STR);
//            result.setResultDesc("尚未购买");
//            return result;
//        }
//        result.setData(eventsInfo);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//
//
//    @ApiOperation(value = "定制推荐购买类型")
//    @RequestMapping(value = "/custom", method = RequestMethod.GET)
//    @CheckLoginAop
//    public ReQueryResult<List<SysConfig>> custom(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo){
//        result = new ReQueryResult();
//        List<SysConfig>list= RedisUtils.getConfigFromRedisByType("00",SysConfig.class);
//
//        QueryWrapper<CustomInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("custom_type");
//        queryWrapper.eq("member_id",memberInfo.getId()).eq("status",TYPE_ZERO);
//        List<CustomInfo> olist=customInfoService.list(queryWrapper);
//
//        for(SysConfig sysConfig:list){
//            sysConfig.setIsPay(TYPE_ONE);
//            for(CustomInfo customInfo:olist){
//                if (customInfo.getCustomType().equals(sysConfig.getCode())){
//                    sysConfig.setIsPay(TYPE_ZERO);
//                    break;
//                }
//            }
//        }
//
//        result.setData(list);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//
//    @ApiOperation(value = "定制推荐列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "index",value = "页码 ",dataType="Integer"),
//            @ApiImplicitParam(name = "length",value = "长度",dataType="Integer"),
//            @ApiImplicitParam(name = "customType",value = "定制类型 ",dataType="String")
//    })
//    @RequestMapping(value = "/custom", method = RequestMethod.POST)
//    @CoerceLoginAop
//    public ReQueryResult<IPage<EventsInfo>> customList(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo, String customType,
//                                    Integer index,Integer length){
//
//        result = new ReQueryResult();
//        QueryWrapper<EventsInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("is_del",TYPE_ONE).eq("type","3");
////        queryWrapper.in("id"," ( select events_id from tb_custom_push_record where  " +
////                " member_id = "+memberInfo.getId()+" custom_type = "+customType+") ");
//        queryWrapper.apply(" id in ( select events_id from re_custom_push_record where  " +
//                " member_id = "+memberInfo.getId()+" and  custom_type = "+customType+") ");
//        queryWrapper.orderByDesc("events_time");
//        Page<EventsInfo> page = new Page<>(index,length);
//        IPage<EventsInfo> list=eis.page(page,queryWrapper);
//        result.setData(list);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//    @RequestMapping(value = "/testEvents", method = RequestMethod.POST)
//    public ReQueryResult testEvents(){
//        result = new ReQueryResult();
//        NutMap map=new NutMap();
//        map.setv("type",1).setv("eventId",null);
//
//        List<NutMap> list=eis.selectEvents(map);
//        for (NutMap nutMap:list){
//            log.info(JSON.toJSONString(nutMap));
//        }
//        result.setData(list);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//
//    //以下为二阶段代码
//
//    @ApiOperation(value = "足球赛事列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "index",value = "页码 ",dataType="Integer"),
//            @ApiImplicitParam(name = "length",value = "长度",dataType="Integer"),
//            @ApiImplicitParam(name = "eventType",value = "状态 1未结束 2已结束 ",dataType="Integer")
//    })
//    @RequestMapping(value = "/competition", method = RequestMethod.POST)
//    public ReQueryResult<IPage<CompetitionRecord>> competitionList(  Integer eventType,Integer type,
//                                                       Integer index,Integer length){
//        result = new ReQueryResult();
//        QueryWrapper<CompetitionRecord> queryWrapper = new QueryWrapper<>();
//        if (eventType==TYPE_ONE){
//            queryWrapper.ne("event_type","完");
//        }else if (eventType==TYPE_TWO){
//            queryWrapper.eq("event_type","完");
//        }
//        queryWrapper.orderByDesc("start_time");
//        Page<CompetitionRecord> page = new Page<>(index,length);
//        IPage<CompetitionRecord> list=competitionRecordService.page(page,queryWrapper);
//        result.setData(list);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//
//
////    @ApiOperation(value = "专号、社区 推荐预测列表")
////    @ApiImplicitParams({
////            @ApiImplicitParam(name = "type",value = "1专号 2社区 ",dataType="Integer"),
////            @ApiImplicitParam(name = "eventType",value = "1足球 2篮球 ",dataType="Integer"),
////            @ApiImplicitParam(name = "index",value = "页码 ",dataType="Integer"),
////            @ApiImplicitParam(name = "length",value = "长度",dataType="Integer"),
////    })
////    @RequestMapping(value = "/forecast", method = RequestMethod.GET)
////    public ReQueryResult<IPage<ForecastInfo>> forecastList( String memberId,Integer type,Integer eventType ,Integer index,Integer length){
////        result = new ReQueryResult();
////        QueryWrapper<ForecastInfo> queryWrapper = new QueryWrapper<>();
////        queryWrapper.eq("type",type).eq("event_type",eventType).orderByDesc("id");
////        Page<ForecastInfo> page = new Page<>(index,length);
////        IPage<ForecastInfo> list=forecastInfoService.page(page,queryWrapper);
////
////
////        StringBuilder sb=new StringBuilder("");
////        String str="";
////        for (ForecastInfo info:list.getRecords()){
////            sb.append(str).append(info.getMemberId());
////            str=",";
////        }
////        NutMap nutMap=new NutMap();
////        nutMap.setv("ids",sb.toString());
////        List<MemberInfoSub> slist2=miss.selectSubList(nutMap);
////        for (ForecastInfo info:list.getRecords()){
////            for (MemberInfoSub sub:slist2){
////                if (info.getMemberId().equals(sub.getMemberId())) {
////                    info.setMemberSub(sub);
////                }
////            }
////        }
////
////        result.setData(list);
////        result.success(SUCCESS_DESC);
////        return result;
////    }
//
//
//    //包含我的发布，查看他人 ，和专号、社区的列表
//    @ApiOperation(value = "专号、社区 推荐预测列表")
//        @ApiImplicitParams({
//                @ApiImplicitParam(name = "type",value = "1专号 2社区 ",dataType="Integer"),
//                @ApiImplicitParam(name = "eventType",value = "1足球 2篮球 ",dataType="Integer"),
//                @ApiImplicitParam(name = "isList",value = "是否列表0 列表 1个人 ",dataType="Integer"),
//                @ApiImplicitParam(name = "isDetail",value = "是否 他人赛事详情 0 是 1 否",dataType="Integer"),
//                @ApiImplicitParam(name = "sort",value = "1胜率 2盈利率 3 连红 4 添加倒序 ",dataType="Integer"),
//                @ApiImplicitParam(name = "index",value = "页码 ",dataType="Integer"),
//                @ApiImplicitParam(name = "length",value = "长度",dataType="Integer"),
//                @ApiImplicitParam(name = "memberId",value = "发布预测的会员id",dataType="Integer")
//     })
//    @RequestMapping(value = "/forecast", method = RequestMethod.GET)
//    @CheckLoginAop
//    public ReQueryResult<List<ForecastInfo>> forecastList(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
//                                                          String memberId,Integer type,Integer eventType ,Integer sort,
//                                                          Integer isList,Integer isDetail,
//                                                          Integer index,Integer length){
//        result = new ReQueryResult();
//        NutMap qmap=new NutMap();
//        qmap.setv("memberId",memberId).setv("type",type).setv("eventType",eventType).setv("isList",isList).setv("sort",sort);
//        Page<ForecastInfo> page = new Page<>(index,length);
//        IPage<ForecastInfo> list=forecastInfoService.getForecastList(page,qmap);
//        NutMap data=new NutMap();
//        NutMap nutMap=new NutMap();
//        data.setv("forecast",list);
//        if (isDetail==0){
//            nutMap.setv("ids",memberId);
//            List<MemberInfoSub> slist=miss.selectSubList(nutMap);
//            nutMap=new NutMap();
//            nutMap.setv("count",TYPE_ZERO).setv("type",TYPE_ONE).setv("memberId",memberInfo.getId()).setv("value",memberId);
//            MemberInfoSub sub=slist.get(0);
//            sub.setIsAttention(checkFocus(nutMap));
//            data.setv("member",sub);
//        }
//        if (isDetail==1&&isList==0){
//            StringBuilder sb=new StringBuilder("");
//            String str="";
//            for (ForecastInfo info:list.getRecords()){
//                sb.append(str).append(info.getMemberId());
//                str=",";
//            }
//            nutMap.setv("ids",sb.toString());
//            List<MemberInfoSub> slist2=miss.selectSubList(nutMap);
//            for (ForecastInfo info:list.getRecords()){
//                for (MemberInfoSub sub:slist2){
//                    if (info.getMemberId().equals(sub.getMemberId())) {
//                        info.setMemberSub(sub);
//                    }
//                }
//            }
//        }
//        result.setData(data);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//
//    @ApiOperation(value = "赛事详情")
//    @ApiImplicitParam(name = "forId",value = "预测详情id",dataType="Integer")
//    @RequestMapping(value = "/forecast/{forId}", method = RequestMethod.GET)
//    @CheckLoginAop
//    public ReQueryResult<ForecastInfo> forecastDetail(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
//                                                            @PathVariable Long forId){
//        result = new ReQueryResult();
//        NutMap nutMap=new NutMap();
//        nutMap.setv("id",forId);
//        nutMap.setv("memberId",memberInfo.getId());
//        ForecastInfo forecastInfo=forecastInfoService.selectForecastInfo(nutMap);
//        if (forecastInfo.getWhether()!=1){
//            forecastInfo.setChargeContent("");
//            forecastInfo.setOdds("");
//        }
//
//        nutMap=new NutMap();
//        nutMap.setv("ids",forecastInfo.getMemberId());
//        List<MemberInfoSub> slist=miss.selectSubList(nutMap);
//
//        nutMap=new NutMap();
//        nutMap.setv("count",TYPE_ZERO).setv("type",TYPE_ONE).setv("memberId",memberInfo.getId()).setv("value",forecastInfo.getMemberId());
//        MemberInfoSub sub=slist.get(0);
//        sub.setIsAttention(checkFocus(nutMap));
//
//        nutMap=new NutMap();
//        nutMap.setv("forecast",forecastInfo).setv("member",sub);
//        result.setData(nutMap);
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//    //检查 查看的用户 是否关注
//    public Integer checkFocus(NutMap nutMap){
//        List<NutMap> flist=focusMemberRecordService.selectFocuseList(nutMap);
//        //focus 0 已关注 1未关注 2 互相关注
//        Integer focus=TYPE_ONE;
//        if (flist.size()>0){
//            NutMap map=flist.get(0);
//            if (map.getInt("count")>=1) {
//                focus=TYPE_ZERO;
//            }
//            if (map.getInt("subCount")>=1) {
//                focus=TYPE_TWO;
//            }
//        }
//        return focus;
//    }
//
//    @ApiOperation(value = "查询今天是否可添加赛事预测")
//    @RequestMapping(value = "/forecastCount", method = RequestMethod.POST)
//    @CoerceLoginAop
//    public ReQueryResult forecastCount(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
//                                                      ForecastInfo info){
//        result = new ReQueryResult();
//        result=checkForecast(memberInfo.getMemberIdentify(),memberInfo.getId());
//        return result;
//    }
//
//    @ApiOperation(value = "添加赛事预测")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "eventId",value = "赛事id 逗号拼接 ",dataType="Integer"),
//            @ApiImplicitParam(name = "eventType",value = "1足球 2篮球 ",dataType="Integer"),
//            @ApiImplicitParam(name = "type",value = "1专号 2社区 ",dataType="Integer"),
//            @ApiImplicitParam(name = "odds",value = "赔率 数组或jsonArray [[6],[6]]",dataType="Integer"),
//    })
//    @RequestMapping(value = "/forecast", method = RequestMethod.POST)
//    @CoerceLoginAop
//    public ReQueryResult forecast(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
//                                                      ForecastInfo info){
//        result = new ReQueryResult();
//        boolean flag = RedisUtils.checkRedisLock("forecast"+memberInfo.getId(), 1);
//        if (!flag) {
//            result.error("请勿重复点击");
//            return result;
//        }
//        result=checkForecast(memberInfo.getMemberIdentify(),memberInfo.getId());
//        if (!"000".equals(result.getResultCode())) {
//            return result;
//        }
//        info.setType(memberInfo.getMemberIdentify());
//        forecastInfoService.save(info);
//
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//
//    public  ReQueryResult checkForecast(Integer identify,Long memberId){
//        QueryWrapper<ForecastInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("member_id",memberId).ne("is_del",TYPE_ZERO)
//                .apply(" date_format(curdate(),'%Y-%m-%d')=date_format(create_time,'%Y-%m-%d') ");
//        Integer count=forecastInfoService.count(queryWrapper);
//        if (identify==TYPE_ONE&&count>=TYPE_THREE){
//            result.error("已达今日发布上限");
//            return result;
//        }
//        if (identify==TYPE_TWO&&count>=TYPE_THREE){
//            result.error("已达今日发布上限");
//            return result;
//        }
//        if (identify>=TYPE_THREE){
//            result.error("无发布权限");
//            return result;
//        }
//        result.success(SUCCESS_DESC);
//        return result;
//    }
//
//
//    @ApiOperation(value = "删除赛事预测")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "forId",value = "发布预测ID ",dataType="Integer"),
//    })
//    @RequestMapping(value = "/forecast/{forId}", method = RequestMethod.DELETE)
//    @CoerceLoginAop
//    public ReQueryResult forecastDel(HttpServletRequest request, @ApiIgnore() MemberInfo memberInfo,
//                                                         @PathVariable Long forId){
//        result = new ReQueryResult();
//        ForecastInfo info=new ForecastInfo();
//        info.setId(forId).setIsDel(TYPE_ZERO);
//        forecastInfoService.updateById(info);
//        result.success(SUCCESS_DESC);
//        return result;
//    }






}

